/*
 *	Accepts		:
 *		targetPath
 *	Requires 	:
 *		targetField 
 * 
 */

Ext.define('AmpleHarvest.views.facetSelector.main',
{
	extend			: 'Ext.window.Window',
	alias			: 'widget.ampleharvest-views-facetselector-main',
	title			: 'Facet Selector',
	layout         	: 'fit',

//	y           	: 0,
	minimizable    	: false,
	maximizable    	: false,
   	closable    	: false,
   	width       	: 600,
   	height      	: Ext.getBody().getViewSize().height * 1/4,
   	shim        	: false,
   	animCollapse   	: false,
	 	
	targetPath		: '/solr-os/',
	targetField		: {},
	
	initComponent	: function()
	{
		var uniqueKey = (((1+Math.random())*0x10000)|0).toString(16).substring(1);
		var bool_hidden = false;
		if(!this.targetField.getValue().length > 0)
		{
			bool_hidden = true;
		}
		this.obj_facetForm = Ext.create('Ext.form.Panel',
				{
					height		: 300,
					requires			:
						[ 
								'AmpleHarvest.util.cPropertiesDropDown',
								'Ext.form.Panel'
						],
					listeners	:
					{
				        setValueField 	: function(str_term)
				        {
				        	Ext.getCmp('ampleharvest-views-facetselector-main-form-value').searchTerm = str_term;;
				        	Ext.getCmp('ampleharvest-views-facetselector-main-form-value').setDisabled(false);
				        },
				        
				        enableSubmit	: function()
				        {
				        	Ext.getCmp('ampleharvest-views-facetselector-main-form-submit').setDisabled(false);
				        }
					},
					
					
					
					items 		: [
					      	Ext.create("Ext.form.field.ComboBox",
					      			{
					      				id				: 'ampleharvest-views-facetselector-main-form-boolean',
					      				fieldLabel		: "Boolean",
										selectOnTab		: true,
										hidden			: bool_hidden,
										displayField	: 'AND',
										store			:
										[
												[
														'AND', 'AND'
												],
												[
														'OR', 'OR'
												]
										]
					      			}
					      		),
							Ext.create("AmpleHarvest.util.cPropertiesDropDown",
									{
										id			: 'ampleharvest-views-facetselector-main-form-property',
										fieldLabel	: "Facets",
										inputType	: "solrfacet",
										fixedFilter	: "string.",
										targetPath	: this.targetPath, // TODO: fix this
										anchor		: '92%',
										emptyText	: 'Select from available facets',
										callbackfn	: 'setValueField' 
									}
								),
							Ext.create("AmpleHarvest.util.cPropertiesDropDown",
									{
										id			: 'ampleharvest-views-facetselector-main-form-value',
										fieldLabel	: "Values",
										disabled	: true,
										inputType	: "solrfacet",
										targetPath	: this.targetPath,
										valueField	: 'facet',
										searchTerm	: 'string',
										anchor		: '92%',
										emptyText	: 'Select Contract ID',
										callbackfn	: 'enableSubmit'
									}
								),

								Ext.create('Ext.button.Button',
										{
											text	: 'Submit',
											id		: 'ampleharvest-views-facetselector-main-form-submit',
											disabled: true,
											scope	: this,
											handler	: function()
											{
												var str_searchString = this.targetField.getValue();
												var obj_form = this.obj_facetForm.getForm().getFields().eachKey(function(str_id, obj_field)
														{
															if(str_id == 'ampleharvest-views-facetselector-main-form-boolean')
															{
																if(!obj_field.hidden)
																{
																	str_searchString += ' '+obj_field.getValue();
																}
															}
															else if (str_id == 'ampleharvest-views-facetselector-main-form-property')
															{
																str_searchString += ' '+obj_field.getValue()+':';
															}	
															else if (str_id == 'ampleharvest-views-facetselector-main-form-value')
															{
																str_searchString += obj_field.getValue();
															}
														});
												this.targetField.setValue(str_searchString);
												m_obj_AmpleHarvest_ViewPort.unmask();
												
												this.destroy();
												
											}
										}
								),
								Ext.create('Ext.button.Button',
										{
											text	: 'Cancel',
											scope	: this,
											handler	: function()
											{
												m_obj_AmpleHarvest_ViewPort.unmask();
												
												this.destroy();
												
											}
										})
										
					      	   ]
		
				});
		
		this.items = [
		              this.obj_facetForm
		              ];

		this.callParent();
	}
});


